export default class Misc {
  public static myLog(message?: any, ...optionalParams: any[]) {
    console.log(message, ...optionalParams);
  }

  public static myWarn(message?: any, ...optionalParams: any[]) {
    console.warn(message, ...optionalParams);
  }

  public static myError(message?: any, ...optionalParams: any[]) {
    console.error(message, ...optionalParams);
  }

  // 获取整形随机数
  public static getRandomInt(min: number, max: number): number {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

  // 获取随机bool
  public static getRandomBoolean() {
    return !!(Misc.getRandomInt(0, 1));
  }

  /**
   * 该方法是对 `Object.create(null)` 的简单封装。这样我们在该对象上查找属性时，就不用进行 `hasOwnProperty` 判断。
   * 在需要频繁判断 `hasOwnProperty` 时，使用这个方法性能会比 `{}` 更高。
   */
  public static createMap(forceDictMode = true) {
    const map = Object.create(null);
    if (forceDictMode) {
      const INVALID_IDENTIFIER_1 = ".";
      const INVALID_IDENTIFIER_2 = "/";
      map[INVALID_IDENTIFIER_1] = true;
      map[INVALID_IDENTIFIER_2] = true;
      delete map[INVALID_IDENTIFIER_1];
      delete map[INVALID_IDENTIFIER_2];
    }
    return map;
  }

  // 快速删除
  public static fastRemoveAt(array: any[], index: number) {
    const length = array.length;
    if (index < 0 || index >= length) {
      return;
    }
    array[index] = array[length - 1];
    array.length = length - 1;
  }
}
